#include "game.h"

using namespace std;

//Constructor
CGame::CGame()
{
    m_eState = MENU;

	//We set SGameSetting 
	m_GetGameSettings.m_uiBoardSize = MSIZE_SMALL;
    m_sGameSettings.m_uiNumberOfPlayers = MAX_PLAYERS;
	m_sGameSettings.m_uiNumberOfRows = MAX_Rows;
	m_sGameSettings.m_uiNumberOfColumns = MAX_COLUMNS;
	m_sGameSettings.m_uiNumberOfLevel = MAX_LEVEL;
	m_sGameSettings.m_uiNumberOfLenght = MAX_LENGHT;
	

	m_pCurrentPlayer = NULL; 
	m_pBoard = NULL;

	//Create EndGameConditions
	m_pEndGameConditions.push_back(new CEndGameConditionsWin(m_sGameSettings.m_TokensToJoin));

	//Crete drawing in GameRenderer
	m_pGameRenderer = new CGameRendererType();

	//Token hasn't been introduced
	m_bTokenInput = false;
}


//Destructor
CGame::~CGame()
{
	delete m_pGameRenderer;
	delete m_pBoard;
}


GAME_SETTINGS CGame::GetGameSettings()
{
	return m_sGameSettings;
}

bool CGame::UpdateGameState(){
    
    switch(m_eState){
        case MENU:
            Menu();
            break;
            
        case INIT:
            Init();
            break;
            
        case PAINT:
            Paint();
            break;
            
        case CHOOSE_X_Y:
            Choose_X_Y();
            break;
            
        case CHECK:
            Check();
            break;

        case CHANGE_TURN:
            Change_Turn();
            break;

        case UPDATE:
            Update();
            break;
            
		case END:
            End();
            break;

        case EXIT:
            Exit(); 
            return true;
            break;
            
        default:
            return true;
    }

    return false;    
} 


void CGame::Menu()
{
    MENU_ITEM l_sMenuItems[1];

	l_sMenuItems[0].m_cOption = 'a';
	l_sMenuItems[0].m_sOptionText = "Nuevo Juego";

	char l_cOption = m_pGameRenderer->PaintMenu(l_sMenuItems, 1, ".:: Game Conect 4 ::.");

	switch(l_cOption){
		case 'y':
			m_eGameState = INIT;
			break;

		case 'n':
			m_eGameState = EXIT;
			break;
	}
}

void CGame::Init()
{
	//Inicializamos el tablero y los jugadores
	if (InitBoard())
	{
		m_pBoard = new CBoard(m_sGameSettings.m_eBoardSize);
		m_pGameRenderer->Board(m_pBoard);
		m_pEndGameConditions[0]->Board(m_pBoard);

		if (InitPlayers())
		{
			//No se ha introducido token
			m_TokenInput = false;

			m_eState = PAINT;
		}
		else
		{
			m_eState = END;
		}
	}
	else
	{
		m_eGameState = END;
	}
}



void CGame::Paint()
{
	m_pGameRenderer->Clear();

	m_pGameRenderer->PaintLine(MAX_LINE, '_');
	cout << endl;

	m_pGameRenderer->CenterText(".:: GameConect 4 ::.", ' ');
	cout << endl;

	m_pGameRenderer->PaintLine(MAX_LINE, '_');
	cout << endl;

	m_pGameRenderer->PaintBoard();

	m_pGameRenderer->PaintLine(MAX_LINE, '_');
	cout << endl;

	if (m_bTokenInput)
	{
		m_eState = CHECK;
	}
	else
	{
		m_eState = INPUT;
	}
    
}



void CGame::Choose_X_Y()
{

}


void CGame::Check()
{
	bool l_bSearchEnd = false;

	for(m_EndGameConditions = m_pEndGameConditions.begin(); m_itEndGameConditions != m_pEndGameConditions.end(); ++m_itEndGameConditions)
	{
		l_bSearchEnd = (*m_itEndGameConditions)->CheckEndGameCondition();

		cout << (*m_itEndGameConditions)->Message();
	}

	if (l_bSearchEnd){
		m_eState = END;
	}
	else{
	    m_eState = CHANGE_TURN;
	}
}




void CGame::Change_Turn()
{
    m_eState = PAINT;
}

void CGame::Update()
{

}

void CGame::End()
{
    m_eState = MENU;
}


void CGame::Exit()
{
    cout << "Hasta luego!!!" << endl;
}
